<?php
namespace Home\Controller;

class SearchController extends HomeController
{
	public function index()
	{
		$search = I('get.search');
		if(empty($search)) {
			$this->assign('search',0);
			$this->display();
			exit;
		}
		$movie = M('movie');
		
		// $data = M()->table(array('lamp_movie'=>'m','lamp_img'=>'i','lamp_category'=>'c'))->field('m.id,m.showdate,m.name as mname,i.icon,c.name as cname')->where('m.id=i.mid and c.id=m.cid and face=1 and m.name like "%'.$search.'%"')->select();
		$count = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon')->join('lamp_img  as  i  on  m.id = i.mid and i.face=1')->where('m.name like "%'.$search.'%"')->count();
		// var_dump($count);exit;
		$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$map['search'] = $_GET['search'];
		// var_dump($map);
		$Page->parameter=$map;
		$show = $Page->show();// 分页显示输出

		 $data = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon')->join('lamp_img  as  i  on  m.id = i.mid and i.face=1')->where('m.name like "%'.$search.'%"')->limit($Page->firstRow.','.$Page->listRows)->select();
		// var_dump($data);exit;
		$this->assign('page',$show);
		$this->assign('search',$data);
		$this->display();
	}


	//首页搜索ajax操作
	public function doajax()
	{
		$search = I('post.search');
		$data = M()->table('lamp_movie  as  m')->field('m.id,m.showdate,m.name as mname,i.icon')->join('lamp_img  as  i  on  m.id = i.mid and face=1')->where('m.name like "%'.$search.'%"')->group('mid')->select();
		$this->ajaxReturn($data);
	}
}